import config from '@/common/request/config.js'
import {
	mapMutations
} from 'vuex';
export default {
	data() {
		return {
			openid: "",
			server: '',
			code: ''
		};
	},
	mounted() {
		if(!uni.getStorageSync('clinetToken')){
			if (process.env.NODE_ENV === 'development') {
				// 开发环境，默认获得用户信息和token
				this.getUserInfo();
			} else {
				// 生产环境走正式的微信支付
				this.getCode()
			}
		}
	},
	methods: {
		...mapMutations(['login']),
		getCode() {
			// 非静默授权，第一次有弹框
			this.code = '';
			const callback_url = config.callback_url; // 获取页面url
			const appid = config.appid;
			const scope = config.scope;
			this.code = this.getUrlCode()?.code; // 截取code
			if (this.code == null || this.code === '') {
				// 如果没有code，则去请求
				window.location.href =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${callback_url}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;
			} else {
				// 当code不等于空时，调用后端接口获取用户信息
				this.getUserInfo();
				// 你自己的业务逻辑
			}
		},
		getUserInfo() {
			this.$u.api.wxH5Login({
				code: this.code
			}).then(result => {
				if (result.code === 200) {
					this.login(result.data);
					setTimeout(async ()=>{
						let urls = await this.deUrlParam(window.location.href, ['code']) // 从url中删除code
						// window.location.href = urls
					},200)
				} else {
					this.$api.msg(result.message);
				}
			})
		},
		getUrlCode() {
			// 截取url中的code方法
			var url = location.search;
			var theRequest = new Object();
			if (url.indexOf('?') != -1) {
				var str = url.substr(1);
				var strs = str.split('&');
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
				}
			}
			return theRequest;
		},
		deUrlParam(url, params){
			/**
			 * 删除URL中指定search参数,会将参数值一起删除
			 * @param {string} url 地址字符串
			 * @param {array} aParam 要删除的参数key数组，如['name','age']
			 * @return {string} 返回新URL字符串
			 */
			for (var index = 0; index < params.length; index++) {
				var item = params[index];
				var fromIndex = url.indexOf(item + "="); //必须加=号，避免参数值中包含item字符串
				if (fromIndex !== -1) {
					// 通过url特殊符号，计算出=号后面的的字符数，用于生成replace正则
					var startIndex = url.indexOf("=", fromIndex);
					var endIndex = url.indexOf("&", fromIndex);
					var hashIndex = url.indexOf("#", fromIndex);
		
					var reg = "";
					if (endIndex !== -1) {
						// 后面还有search参数的情况
						var num = endIndex - startIndex;
						reg = new RegExp(item + "=.{" + num + "}");
						url = url.replace(reg, "");
					} else if (hashIndex !== -1) {
						// 有hash参数的情况
						var num = hashIndex - startIndex - 1;
						reg = new RegExp("&?" + item + "=.{" + num + "}");
						url = url.replace(reg, "");
					} else {
						// search参数在最后或只有一个参数的情况
						reg = new RegExp("&?" + item + "=.+");
						url = url.replace(reg, "");
					}
				}
			}
			var noSearchParam = url.indexOf("=");
			if (noSearchParam === -1) {
				url = url.replace(/\?/, ""); // 如果已经没有参数，删除？号
			}
			return url;
		}
	}

}